package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/6 14:16
 * @description：规格组业务层
 */
@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    @Autowired
    private SpecParamService specParamService;
    @Override
    public List<SpecGroupDTO> querySpecGroupByCategory(Long id) {
        //baseMapper.querySpecGroupByCategory(id);
        List<SpecGroup> specGroups = this.query().eq("category_id", id).list();
        List<SpecGroupDTO> specGroupDTOList = SpecGroupDTO.convertEntityList(specGroups);
        return specGroupDTOList;
    }

    @Override
    public List<SpecGroupDTO> querySpecGroupAndParamByCategoryId(Long id) {
        /*//baseMapper.querySpecGroupAndParamByCategoryId(id);
        List<SpecGroup> specGroups = this.query().eq("category_id", id).list();
        List<SpecGroupDTO> specGroupDTOList = SpecGroupDTO.convertEntityList(specGroups);
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            List<SpecParam> specParams = specParamService.query().eq("category_id", id).list();
            List<SpecParamDTO> specParamDTOList = SpecParamDTO.convertEntityList(specParams);
            specGroupDTO.setParams(specParamDTOList);
        }
        return specGroupDTOList;*/

        List<SpecGroupDTO> specGroupDTOS = SpecGroupDTO.convertEntityList(
                this.query()
                        .eq("category_id", id)
                        .list());


        //key groupId,value,相同groupId对应的规格参数集合
        Map<Long, List<SpecParamDTO>> specParaMap = SpecParamDTO.convertEntityList(
                this.specParamService.query()
                        .eq("category_id", id)
                        .list())
                .stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        //循环给每个组赋值组内规格参数集合
        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(specParaMap.get(specGroupDTO.getId()));
        });
        return specGroupDTOS;

    }

    @Override
    @Transactional
    public void addSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        this.save(specGroup);
    }

    @Override
    @Transactional
    public void updateSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        this.updateById(specGroup);
    }


}
